#
# parse linux version string
#
LNX_HDR_VER	:= $(call qstrip,$(CONFIG_KER_VER))
LNXVER		:= $(subst ., , $(LNX_HDR_VER))
VERSION		:= $(word 1, $(LNXVER))
PATCHLEVEL	:= $(word 2, $(LNXVER))
SUBLEVEL 	:= $(word 3, $(LNXVER))
EXTRAVERSION 	:= $(word 4, $(LNXVER))
LOCALVERSION:=

# should contain prepended dot
EXTRAVERSION:=$(if $(EXTRAVERSION),.$(EXTRAVERSION),)

LNX_HDR_SITE:=$(BR2_KERNEL_MIRROR)/linux/kernel/v2.6/
LNX_PKG_SRC:=linux-$(LNX_HDR_VER).tar.bz2
LNX_HDR_UNPACK_DIR:=$(TOOL_BUILD_DIR)/linux-$(LNX_HDR_VER)
PKG_HDR_DIR:=$(TOOL_BUILD_DIR)/linux

LINUX_HEADERS_DEPENDS:=

$(LNX_HDR_UNPACK_DIR)/.unpacked: kernel-headers-source
	@echo $@
	rm -rf $(PKG_HDR_DIR)
	[ -d $(TOOL_BUILD_DIR) ] || $(INSTALL) -d $(TOOL_BUILD_DIR)
	[ -d $(LNX_HDR_UNPACK_DIR) ] || \
		tar -C $(TOOL_BUILD_DIR) $(TAR_OPTIONS) $(DL_DIR)/$(LNX_PKG_SRC)
	touch $@

$(LNX_HDR_UNPACK_DIR)/.patched: $(LNX_HDR_UNPACK_DIR)/.unpacked $(LINUX_HEADERS_DEPENDS)
	scripts/qpatch.sh $(LNX_HDR_UNPACK_DIR) kernel/$(LNX_HDR_VER)
	touch $@

$(PKG_HDR_DIR)/.configured: $(LNX_HDR_UNPACK_DIR)/.patched
	@rm -f $(PKG_HDR_DIR)
	ln -s $(LNX_HDR_UNPACK_DIR) $(PKG_HDR_DIR)
	touch $@

#	(cd $(LNX_HDR_UNPACK_DIR); \
#	 $(MAKE) ARCH=$(ARCH) \
#		HOSTCC="$(HOSTCC)" HOSTCFLAGS="$(HOSTCFLAGS)" \
#		HOSTCXX="$(HOSTCXX)" \
#		INSTALL_HDR_PATH=$(PKG_HDR_DIR) headers_install; \
#	)
#
#
# template 
#
kernel-headers: $(PKG_HDR_DIR)/.configured
	@echo $@

kernel-headers-source:
	$(call DOWNLOAD,$(LNX_HDR_SITE),$(LNX_PKG_SRC))
	@echo $@

kernel-headers-clean: clean
	rm -rf $(PKG_HDR_DIR)

kernel-headers-dirclean:
	rm -rf $(PKG_HDR_DIR)
	rm -rf $(LNX_HDR_UNPACK_DIR)

# export rules #
#.PHONY: kernel-headers
